package yannuo.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import yannuo.service.impl.EmpServiceImpl;
import yannuo.util.PageUtils;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/pageEmps")
public class PageQueryEmpServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNum = request.getParameter("pageNum");
        int pageNumber = 0;
        if(pageNum!=null && !pageNum.equals("")){
            pageNumber = Integer.parseInt(pageNum);
        }

        String pageSize = request.getParameter("pageSize");
        int pageSizeCount = 0;
        if(pageSize!=null && !pageSize.equals("")){
            pageSizeCount = Integer.parseInt(pageSize);
        }

        //调用业务层方法
        PageUtils pageUtils = new EmpServiceImpl().listEmp(pageNumber, pageSizeCount);
        //转JSON数据格式
        ObjectMapper om = new ObjectMapper();
        String s = om.writeValueAsString(pageUtils);
        PrintWriter pw = response.getWriter();
        pw.print(s);
        pw.close();

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
